//
//  PhotoView.m
//  微博2期
//
//  Created by J on 15/12/5.
//
//

#import "PhotoView.h"
#import "Photo.h"
#import "UIImageView+WebCache.h"
@interface PhotoView()
@property(nonatomic,weak)UIImageView * gifView;
@end
@implementation PhotoView

- (UIImageView *)gifView{
    
    if (_gifView) {
        UIImage * image = [UIImage imageNamed:@"timeline_image_gif"];
        UIImageView* gifView =[[UIImageView alloc]initWithImage:image];
        self.gifView = gifView;
    }
    
    return _gifView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.gifView];
//        self.backgroundColor = [UIColor orangeColor];
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;//超出边框内容不显示
        
        
    }
    return self;
}

//typedef NS_ENUM(NSInteger, UIViewContentMode) {
//    UIViewContentModeScaleToFill,  //默认 伸缩填充整个imageView
//    UIViewContentModeScaleAspectFit,   //保证图片全部在里面 要看到所有内容 但是又要伸缩到保持原来宽高比 直到宽度或者高度刚好接触到imageView边缘   // contents scaled to fit with fixed aspect. remainder is transparent
//    UIViewContentModeScaleAspectFill,     //不保证图片全部在里面 要伸缩到保持原来宽高比 直到宽度或者高度刚好接触到imageView边缘 // contents scaled to fill with fixed aspect. some portion of content may be clipped.
//    UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
//    UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
//    UIViewContentModeTop,
//    UIViewContentModeBottom,
//    UIViewContentModeLeft,
//    UIViewContentModeRight,
//    UIViewContentModeTopLeft,
//    UIViewContentModeTopRight,
//    UIViewContentModeBottomLeft,
//    UIViewContentModeBottomRight,
//};

//规律  fill填充整个imageView
//1.凡是带有Scale 图片都会拉伸
//2.凡是带有Aspect单词的，图片都会保持原来的宽高比 图片不会变形
//3.


- (void)setPhoto:(Photo *)photo{
    _photo = photo;    
    //设置图片
    [self sd_setImageWithURL:[NSURL URLWithString:photo.thumbnail_pic] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
//    显示隐藏gif控件

    //判断是否以gif结尾 GIF 全部转换为小写对比
    self.gifView.hidden = ![photo.thumbnail_pic.lowercaseString hasSuffix:@"gif"];//不是gif结尾隐藏

    
}



- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.gifView.x = self.width - self.gifView.width;
    self.gifView.y = self.gifView.height;
}



@end
